<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <title th:replace="common/base::pagetitle"></title>
    <link th:replace="common/base::static"/>
    <link rel="stylesheet" th:href="@{/static/plugins/easyui/themes/material/easyui.css}">
    <link rel="stylesheet" th:href="@{/static/plugins/easyui/themes/icon.css}">
    <link rel="stylesheet" th:href="@{/static/css/kvf-treegrid.css}">
</head>
<body style="background-color: #f2f2f2;">

<div id="kPage" class="layui-fluid" style="padding: 0">
    <div class="layui-row">
        <div class="layui-col-md12" id="kMainContentCol">
            <div class="layui-fluid kvf-fluid-padding15">
                <div class="layui-card">
                    <form>
                        <div class="layui-form layui-card-header kvf-card-header-auto">
                            <div class="layui-form-item">
                                <div class="layui-inline">
                                    <label class="layui-form-label">字典名称</label>
                                    <div class="layui-input-inline">
                                        <input type="text" name="name" placeholder="请输入字典名称" autocomplete="off" class="layui-input">
                                    </div>
                                </div>

                                <div class="layui-inline">
                                    <button class="layui-btn " lay-submit="" lay-filter="kTableSearchFilter">
                                        <i class="layui-icon layui-icon-search"></i>
                                    </button>
                                    <button class="layui-btn " type="reset" data-event="refresh">
                                        <i class="layui-icon layui-icon-refresh-1"></i>
                                    </button>
                                </div>
                            </div>
                        </div>
                    </form>

                    <div class="layui-card-body">
                        <table id="kTreeGrid" style="height: 545px"></table>
                    </div>
                </div>

            </div>
        </div>
    </div>
</div>


<script th:replace="common/base::context"></script>
<script th:replace="common/base::mainjs"></script>
<script th:src="@{/static/plugins/easyui/easyloader.js}"></script>

<!--头部工具栏模板-->
<div type="text/html" id="headToolbarTpl" class="layui-btn-container" style="padding: 6px 0 0 6px">
    <button class="layui-btn layui-btn-sm " data-event="add">添加</button>
    <button class="layui-btn layui-btn-sm layui-btn-danger" data-event="batchdel">批量删除</button>
</div>

<!--表格行工具栏模板-->
<script type="text/html" id="rowToolbarTpl">
    {{# if(d.type === 0) { }}
    <a class="layui-btn layui-btn-xs layui-btn-green" data-event="add" onclick="active.add(this)" data-id="{{d.id}}"><i class="layui-icon layui-icon-edit"></i>添加</a>
    {{# } }}
    <a class="layui-btn layui-btn-xs layui-btn-normal" data-event="edit" onclick="active.edit(this)" data-id="{{d.id}}"><i class="layui-icon layui-icon-edit"></i>编辑</a>
    <a class="layui-btn layui-btn-xs layui-btn-danger" data-event="del" onclick="active.del(this)" data-id="{{d.id}}"><i class="layui-icon layui-icon-delete"></i>删除</a>
</script>

<!--表格列格式化模板 start-->
<script type="text/html" id="typeTpl">
    {{#  if(d.type == 0){ }}
    <span class="layui-badge layui-bg-blue">字典类别</span>
    {{# } else if(d.type == 1) { }}
    <span class="layui-badge layui-bg-orange">字典项</span>
    {{# } }}
</script>
<script type="text/html" id="statusTpl">
    {{#  if(d.status == 0){ }}
    <span class="layui-badge layui-bg-green">有效</span>
    {{# } else if(d.status == 1) { }}
    <span class="layui-badge layui-bg-gray">无效</span>
    {{# } }}
</script>
<!--表格列格式化模板 end-->

<script type="text/javascript" th:inline="none">
    var active;
    layui.use(['element', 'form', 'layer', 'table', 'laydate'], function () {
        var elemet = layui.element, form = layui.form, layer = layui.layer, laydate = layui.laydate;
        var treeGridId = 'kTreeGrid';
        var area = ['433px', '95%'];

        var treeGridInst;
        easyloader.load('treegrid', function () {

            treeGridInst = $('#' + treeGridId).treegrid($.extend(config.easyui.treegrid, {
                url: BASE_WEB + 'sys/dict/list/data', // 数据接口
                method: req.type.get,
                idField: 'id',
                treeField: 'name',
                toolbar: '#headToolbarTpl',
                onExpand: function (row) {
                    treeGridKit.fitRightFrozenCol(treeGridInst);
                },
                onCollapse: function (row) {
                    treeGridKit.fitRightFrozenCol(treeGridInst);
                },
                loadFilter: function (r) {
                    // todo 请自行实现：树表格第一层不设置_parentId即可，其它层级都要设置_parentId
                    var rows = r.data;
                    r.rows = rows;
                    var rootType;
                    for (var i = 0; i < rows.length; i++) {
                        if (r.type === 'q') {
                            if (i === 0) {
                                rootType = rows[i].type;
                            }
                            if (rows[i].type !== rootType) {
                                rows[i]._parentId = rows[i].parentId;
                            }
                        } else {
                            if (rows[i].parentId !== 0) {
                                rows[i]._parentId = rows[i].parentId;
                            }
                        }

                        if (rows[i].type === 1) {
                            rows[i].state = "open";
                        } else {
                            rows[i].state = "closed";
                        }
                    }
                    return r;
                },
                onLoadSuccess: function (row, data) {
                    treeGridKit.fitRightFrozenCol(treeGridInst);
                },
                frozenColumns: [[
                    {field: 'toolbar', title: '操作', width: 200, align: 'center', fixed: true, formatter: formatter.rowToolbar}
                ]],
                columns: [[
                    {field: 'name', title: '字典名称', width:200, fixed: true}
                    ,{field: 'type', title: '类型', width:100, align: 'center', formatter: formatter.type, fixed: true}
                    ,{field: 'code', title: '字典码', width:150, fixed: true}
                    ,{field: 'value', title: '字典值', width:100, align: 'center', fixed: true}
                    ,{field: 'status', title: '状态', width:80, align: 'center', formatter: formatter.status, fixed: true}
                    ,{field: 'sort', title: '排序值', width:130, align: 'center', fixed: true}
                    ,{field: 'remark', title: '备注', width:130, fixed: false}
                ]]

            }));

            treeGridKit.setPager(treeGridInst);
        });

        // 监听事件
        active = {
            add: function (that) {
                var id = $(that).data('id');
                id = id || 0;
                var level = treeGridInst.treegrid('getLevel', id);

                layer.open({
                    type: 2
                    ,title: '添加'
                    ,content: BASE_WEB + 'sys/dict/edit' + '?parentId=' + id + '&type=' + level
                    ,maxmin: true
                    ,area: area   // 宽高
                    ,btn: ['确定', '取消']
                    ,yes: function(index, layero){
                        //点击确认触发 iframe 内容中的按钮提交
                        var submit = layero.find('iframe').contents().find("#kFormSubmit");
                        submit.click();
                    }
                });
            },
            batchdel: function (that) {
                var checkData = treeGridInst.treegrid('getCheckedNodes');
                if(checkData.length === 0){
                    return kvfKit.warningMsg('请选择数据');
                }
                layer.confirm('确定删除吗？', function(index) {
                    var ids = [];
                    $.each(checkData, function(i, n){
                        ids[i] = n.id;
                    });

                    //执行 Ajax 后重载
                    $.ajax({
                        type: req.type.post,
                        url: BASE_WEB + 'sys/dict/batchdel',
                        data: {ids: ids.toString()},
                        success: function (r) {
                            if (r.code === req.status.ok) {
                                treeGridKit.reload(treeGridInst);
                                kvfKit.sucessMsg('已删除');
                            } else {
                                kvfKit.errorMsg(r.msg);
                            }
                        }
                    });
                });
            },

            edit: function (that) {
                var id = $(that).data('id');
                layer.open({
                    type: 2
                    ,title: '编辑'
                    ,content:  BASE_WEB + 'sys/dict/edit?id=' + id
                    ,maxmin: true
                    ,area: area   // 宽高
                    ,btn: ['确定', '取消']
                    ,yes: function(index, layero){
                        //点击确认触发 iframe 内容中的按钮提交
                        var submit = layero.find('iframe').contents().find("#kFormSubmit");
                        submit.click();
                    }
                });
            },
            del: function (that) {
                var id = $(that).data('id');
                layer.confirm('确定删除吗？', function(index) {
                    // 执行 Ajax 后重载
                    $.ajax({
                        type: req.type.post,
                        url: BASE_WEB + 'sys/dict/del/' + id,
                        async: false,
                        success: function (r) {
                            if (r.code === req.status.ok) {
                                treeGridKit.reload(treeGridInst);
                                kvfKit.sucessMsg('已删除');
                            } else {
                                kvfKit.errorMsg(r.msg);
                            }
                        }
                    });
                });
            },
            refresh: function () {
                renderTableData();
            }
        };

        kvfKit.activeInit(active);

        $(window).resize(function () {
            treeGridKit.fitRightFrozenCol(treeGridInst);
        });

        // 列格式化函数
        var formatter = {
            rowToolbar: function (value, row, index) {
                return kvfKit.renderTpl('rowToolbarTpl', row);
            },
            type: function (value, row, index) {
                return kvfKit.renderTpl('typeTpl', row);
            },
            status: function (value, row, index) {
                return kvfKit.renderTpl('statusTpl', row);
            }
        };


        // 监听查询
        form.on('submit(kTableSearchFilter)', function (obj) {
            renderTableData(obj.field);
            return false;
        });

        function renderTableData(params) {
            params = params ? params : {};
            log('seach=', params); //

            $.get(BASE_WEB + 'sys/dict/list/data', params, function(r) {
                r.type = 'q';
                treeGridInst.treegrid('loadData', r);
            }, 'json');
        }

        // 绑定监听回车事件
        kvfKit.bindEnterEventForLayuiForm();

    });
</script>

</body>
</html>